<template>
  <div id="app">
    <el-backtop>UP</el-backtop>
    <!-- 顶部导航栏 -->
    <div class="topbar">
      <div class="nav">
        <ul>
          <li v-if="!this.$store.getters.getUser">
            <el-button type="text" @click="open">登录</el-button>
            <span class="sep">|</span>
            <el-button type="text" @click="register = true">注册</el-button>
          </li>
          <li v-else>
            欢迎您
            <el-popover placement="top" width="180" v-model="visible">
              <p>确定退出登录吗？</p>
              <div style="text-align: right; margin: 10px 0 0">
                <el-button size="mini" type="text" @click="visible = false">取消</el-button>
                <el-button type="primary" size="mini" @click="logout">确定</el-button>
              </div>
              <el-button type="text" slot="reference">{{ this.$store.getters.getUser.username }}</el-button>
            </el-popover>
          </li>
          <li>
            <router-link to="/personalcenter">个人中心</router-link>
          </li>
          <li>
            <router-link to="/collect">我的收藏</router-link>
          </li>
          <li>
            <router-link to="/shoppingcart">
              <i class="el-icon-shopping-cart-full"></i> 购物车
              <span class="num">({{ getNum }})</span>
            </router-link>
          </li>
        </ul>
      </div>

    </div>
    <!-- 顶部导航栏END -->

    <!-- 顶栏容器 -->
    <el-header>

      <el-menu
          :default-active="activeIndex"
          class="el-menu-demo"
          mode="horizontal"
          active-text-color="#409eff"
          router
      >
        <el-menu-item index="/">首页</el-menu-item>
        <el-menu-item index="/commodity">全部商品</el-menu-item>
        <el-menu-item index="/about">关于我们</el-menu-item>

        <div class="so">
          <el-input placeholder="请输入搜索内容" v-model="searchInput">
            <el-button slot="append" icon="el-icon-search" @click="quickSearch"></el-button>
          </el-input>
        </div>
      </el-menu>
    </el-header>
    <!-- 顶栏容器END -->

    <!--登录模块-->
    <Login></Login>
    <!--注册模块-->
    <Regist :register="register" @fromChild="isRegister"></Regist>


    <el-main>
      <keep-alive>
        <!--transition添加切换动画-->
        <transition name="fade">
          <router-view></router-view>
        </transition>
      </keep-alive>
    </el-main>


    <!-- 底栏容器 -->
    <el-footer>
      <div class="footer">
        <div class="ng-promise-box">
          <div class="ng-promise">
            <p class="text">
              <a class="icon1" href="javascript:;">7天无理由退换货</a>
              <a class="icon2" href="javascript:;">满99元全场免邮</a>
              <a class="icon3" style="margin-right: 0" href="javascript:;">100%品质保证</a>
            </p>
          </div>
        </div>
        <div class="mod_help">
          <p>
            <router-link to="/">首页</router-link>
            <span>|</span>
            <router-link to="/">全部商品</router-link>
            <span>|</span>
            <router-link to="/about">关于我们</router-link>
          </p>
          <p class="coty">商城版权所有 &copy; 2020-2021</p>
        </div>
      </div>
    </el-footer>
    <!-- 底栏容器END -->
  </div>
</template>

<script>

import {mapActions} from "vuex";
import {mapGetters} from "vuex";
import myStorage from "./utils/myStorage";
import {removeCookie, setCookie} from "./utils/myCookie";

export default {
  data() {
    return {
      excludeRoutes: ['HomeIndex', 'MyAddress', 'AddAddress', 'MyOrder', 'MyShoppingCart'],
      activeIndex: "",// 头部导航栏选中的标签
      register: false, // 是否显示注册组件
      visible: false, // 是否退出登录
      searchInput: "",//搜索框
    }
  },
  computed: {
    ...mapGetters(["getUser", "getNum"])
  },
  watch: {
    getUser: function (val) {
      if (val === "") {//用户没有登录
        this.setShoppingCart([]);
      } else {
        let params = new URLSearchParams();
        params.append("userId", this.$store.getters.getUser.id);
        this.$axios
            .post("/member/cartList", params)
            .then(res => {
              this.setShoppingCart(res.data.result);
              // this.notifySucceed("获取购物车信息成功");
            })
            .catch(err => {
              return Promise.reject(err);
            });
      }
    },
  },
  methods: {
    ...mapActions(["setUser", "setUserToken", "setShowLogin", "setShoppingCart", "setUserAddress"]),
    open() {
      // 点击登录按钮, 通过更改vuex的showLogin值显示登录组件
      this.setShowLogin(true);
    },
    // 接收注册子组件传过来的数据
    isRegister(val) {
      this.register = val;
    },
    //退出登录
    logout() {
      let userToken = this.$store.getters.getUser.token;
      this.$axios
          .get("/member/logout", {
            params: {
              token: userToken,
            }
          })
          .then(res => {
            if (res.data.code == "200") {
              // 清空vuex登录信息
              this.setUser("");
              myStorage.clear();//清除本地所有缓存
              this.notifySucceed("退出登录成功");
              this.visible = false;
              this.$router.replace("/");
            } else {
              this.notifyError("退出失败");
            }
          })
          .catch(err => {
            return Promise.reject(err);
          });
    },
    //快速搜索
    quickSearch() {
      this.$axios
          .get("/goods/quickSearch", {
            params: {
              "key": this.searchInput
            }
          })
          .then(res => {
            // removeCookie("SearchItem")
            // removeCookie("searchInput")
            // setCookie("SearchItem",res.data.result,1)
            // setCookie("searchInput",this.searchInput,1)
            myStorage.removeItem("SearchItem");
            myStorage.removeItem("searchInput");
            myStorage.setItem({name: "SearchItem", value: res.data.result});
            myStorage.setItem({name: "searchInput", value: this.searchInput});
            this.$router.push("/commoditysearch");
            this.$message.success('数据加载中')
            this.searchInput = '';
          })
          .catch(err => {
            return Promise.reject(err);
          });
    },
  },
  // mounted: function () {
  //   //此方法刷新页面时也会执行
  //   window.addEventListener('beforeunload',()=>{
  //     myStorage.clear();
  //   });
  // },
  created() {
    if (myStorage.getItem("userToken")) {
      this.$axios
          .get("/member/checkLogin", {
            params: {
              "token": eval("(" + myStorage.getItem("userToken") + ")")
            }
          })
          .then(res => {
            if (res.data.result.message === null){
              this.setUser(res.data.result);
            }else{
              this.setUser(res.data.result);
              this.notifyError(res.data.result.message);
            }
          })
          .catch(err => {
            return Promise.reject(err);
          })
    }
    document.body.removeChild(document.getElementById('Loading'));// 加载页面完后移除加载动画
  },
}
</script>

<style>

/* 全局CSS */
* {
  padding: 0;
  margin: 0;
  border: 0;
  list-style: none;
}

#app .el-header {
  padding: 0;
}

#app .el-main {
  min-height: 300px;
  padding: 20px 0;
}

#app .el-footer {
  padding: 0;
}

a,
a:hover {
  text-decoration: none;
}

/* 全局CSS END */

/* 顶部导航栏CSS */
.topbar {
  height: 40px;
  background-color: #3d3d3d;
  margin-bottom: 20px;
}

.topbar .nav {
  max-width: 1225px;
  margin: 0 auto;
}

.topbar .nav ul {
  float: right;
}

.topbar .nav li {
  float: left;
  height: 40px;
  color: #b0b0b0;
  font-size: 14px;
  text-align: center;
  line-height: 40px;
  margin-left: 20px;
}

.topbar .nav .sep {
  color: #b0b0b0;
  font-size: 12px;
  margin: 0 5px;
}

.topbar .nav li .el-button {
  color: #b0b0b0;
}

.topbar .nav .el-button:hover {
  color: #fff;
}

.topbar .nav li a {
  color: #b0b0b0;
}

.topbar .nav a:hover {
  color: #fff;
}

.topbar .nav .shopCart {
  width: 120px;
  background: #424242;
}

.topbar .nav .shopCart:hover {
  background: #fff;
}

.topbar .nav .shopCart:hover a {
  color: #ff6700;
}

.topbar .nav .shopCart-full {
  width: 120px;
  background: #ff6700;
}

.topbar .nav .shopCart-full a {
  color: white;
}

/* 顶部导航栏CSS END */

/* 顶栏容器CSS */
.el-header .el-menu {
  max-width: 1225px;
  margin: 0 auto;
}

.el-header .logo {
  height: 60px;
  width: 200px;
  float: left;
}

.el-header .so {
  margin-top: 10px;
  max-width: 300px;
  float: right;
}

/* 顶栏容器CSS END */

/* 底栏容器CSS */
.footer {
  width: 100%;
  text-align: center;
  background: #2f2f2f;
  padding-bottom: 20px;
}

.footer .ng-promise-box {
  border-bottom: 1px solid #3d3d3d;
  line-height: 145px;
}

.footer .ng-promise-box {
  margin: 0 auto;
  border-bottom: 1px solid #3d3d3d;
  line-height: 145px;
}

.footer .ng-promise-box .ng-promise p a {
  color: #fff;
  font-size: 20px;
  margin-right: 210px;
  padding-left: 44px;
  height: 40px;
  display: inline-block;
  line-height: 40px;
  text-decoration: none;
}

.footer .github {
  height: 50px;
  line-height: 50px;
  margin-top: 20px;
}

.footer .github .github-but {
  width: 50px;
  height: 50px;
  margin: 0 auto;
}

.footer .mod_help {
  text-align: center;
  color: #888888;
}

.footer .mod_help p {
  margin: 20px 0 16px 0;
}

.footer .mod_help p a {
  color: #888888;
  text-decoration: none;
}

.footer .mod_help p a:hover {
  color: #fff;
}

.footer .mod_help p span {
  padding: 0 22px;
}

/* 底栏容器CSS END */

/*router切换动画效果*/
.fade-enter-active, .fade-leave-active {
  transition: opacity .7s;
}

/* .fade-leave-active below version 2.1.8 */
.fade-enter, .fade-leave-to {
  opacity: 0;
}

/*router切换动画效果END*/
</style>
